package org.example.classtopia;

import jakarta.annotation.PostConstruct;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@MapperScan("org.example.classtopia.Mapper")
@EnableScheduling
public class ClassTopiaApplication {

    @Autowired
    private ApplicationContext applicationContext;

    public static void main(String[] args) {
        SpringApplication.run(ClassTopiaApplication.class, args);
    }

    @PostConstruct
    public void checkBeans() {
        try {
            System.out.println("=== 检查 MyBatis 相关 Bean ===");

            // 检查 DataSource
            if (applicationContext.containsBean("dataSource")) {
                System.out.println("✓ DataSource bean 存在");
            } else {
                System.out.println("✗ DataSource bean 不存在");
            }

            // 检查 SqlSessionFactory
            if (applicationContext.containsBean("sqlSessionFactory")) {
                System.out.println("✓ SqlSessionFactory bean 存在");
            } else {
                System.out.println("✗ SqlSessionFactory bean 不存在");
            }

            // 列出所有 Mapper beans
            String[] mapperBeans = applicationContext.getBeanNamesForType(
                    org.apache.ibatis.annotations.Mapper.class);
            System.out.println("Mapper beans 数量: " + mapperBeans.length);
            for (String bean : mapperBeans) {
                System.out.println("  - " + bean);
            }

            System.out.println("=== 检查 Spring AI 相关 Bean ===");

            // 检查 ChatClient
            if (applicationContext.containsBean("chatClient")) {
                System.out.println("✓ ChatClient bean 存在");
            } else {
                System.out.println("✗ ChatClient bean 不存在");
            }

            // 检查 ChatMemory
            if (applicationContext.containsBean("chatMemory")) {
                System.out.println("✓ ChatMemory bean 存在");
            } else {
                System.out.println("✗ ChatMemory bean 不存在");
            }

            // 检查 OpenAiChatModel
            if (applicationContext.containsBean("openAiChatModel")) {
                System.out.println("✓ OpenAiChatModel bean 存在");
            } else {
                System.out.println("✗ OpenAiChatModel bean 不存在");
            }

            // 检查 chatMemoryRepository 类型
            if (applicationContext.containsBean("chatMemoryRepository")) {
                Object repo = applicationContext.getBean("chatMemoryRepository");
                System.out.println("chatMemoryRepository类型: " + repo.getClass().getName());
            } else {
                System.out.println("✗ chatMemoryRepository bean 不存在");
            }

        } catch (Exception e) {
            System.out.println("检查 Bean 时发生错误: " + e.getMessage());
        }
    }

    @Bean
    public HealthIndicator customHealthIndicator() {
        return () -> {
            try {
                return Health.up()
                        .withDetail("app", "ClassTopia Backend")
                        .withDetail("status", "运行正常")
                        .build();
            } catch (Exception e) {
                return Health.down()
                        .withDetail("error", e.getMessage())
                        .build();
            }
        };
    }
}
